package com.dyh.quickdemozwei.ws;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

@Slf4j
@ServerEndpoint(value = "/helloWs",
        decoders = {HelloWebSocketServer.HelloDecoder.class},
        encoders = {HelloWebSocketServer.HelloEncoder.class})

@Component
public class HelloWebSocketServer {

    Thread worker;



    @OnOpen
    public void onOpen(Session session){
//        SessionSet.add(session);
//        OnlineCount.incrementAndGet();  // 在线数+1
//        SendMessage(session,"连接成功");    // 告知客户端连接成功
        log.info("链接成功");
    }

    /**
     * 连接关闭调用方法
     */
    @OnClose
    public void onClose(Session session){
        log.info("链接关闭");
    }

    @Setter@Getter@AllArgsConstructor@NoArgsConstructor
    public static class HelloMsg{
        int code;
        String data;
    }

    class ReportThread extends Thread{

    }

    public  static class HelloEncoder implements Encoder.Text<HelloMsg> {

        public String encode(HelloMsg object) throws EncodeException {
            return JSON.toJSONString(object);
        }

        @Override
        public void init(EndpointConfig endpointConfig) {

        }

        @Override
        public void destroy() {

        }
    }

    public static class HelloDecoder implements Decoder.Text<HelloMsg> {

        @Override
        public HelloMsg decode(String move) {
            return JSON.parseObject(move, HelloMsg.class);
        }


        @Override
        public boolean willDecode(String s) {
            return true;
        }

        @Override
        public void init(EndpointConfig endpointConfig) {

        }

        @Override
        public void destroy() {

        }
    }
    @OnMessage
    public void onMessage(HelloMsg request, Session session){
        log.info("收到请求 {}",JSON.toJSONString(request));

        if (worker==null){
            worker = new Thread(()->{
                try {
                    Thread.sleep(1200L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
//                    session.getBasicRemote().sendText("{\"code\":200,\"data\":\"s1\"}");

                    session.getBasicRemote().sendObject(new HelloMsg(200,"s1"));
                } catch (Exception e) {
                    e.printStackTrace();
                }


                try {
                    Thread.sleep(1500L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    session.getBasicRemote().sendObject(new HelloMsg(200,"s2"));

                } catch (Exception e) {
                    e.printStackTrace();
                }


                try {
                    Thread.sleep(2000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    session.getBasicRemote().sendObject(new HelloMsg(200,"s3"));

                } catch (Exception e) {
                    e.printStackTrace();
                }
                //
                try {
                    session.close();
                    log.info("关闭成功");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
        worker.start();
    }

}
